import tkinter as tk
from tkinter import ttk
from conf import conf


class ConsoleLayout:
    def __init__(self, console_view):
        # 控制台视图类
        self.console_view = console_view
        # 父组件
        self.parent_frame = self.console_view.frame
        # 动态组件
        self.entry_index = None
        self.label_all_index = None
        self.combobox_time = None
        self.entry_signal = None
        self.label_all_signal = None
        self.entry_message = None

        # 动态组件关联变量
        self.entry_index_var = tk.StringVar()
        self.label_all_index_var = tk.StringVar()
        self.combobox_time_var = tk.StringVar()
        self.entry_signal_var = tk.StringVar()
        self.label_all_signal_var = tk.StringVar()
        self.combobox_crosshair_var = tk.StringVar()
        self.entry_time_span_var = tk.StringVar()
        self.entry_change_range_var = tk.StringVar()
        self.message_file_var = tk.StringVar()
        self.entry_message_var = tk.StringVar()
        self.entry_message_show_var = tk.StringVar()
        self.entry_move_unit_var = tk.StringVar()

        # 数据看板相关变量
        self.symbol_var = tk.StringVar()
        self.price_var = tk.StringVar()
        pass

    def layout(self):
        # 绑定全局键盘事件
        self.console_view.super_view.root.bind("<Up>", lambda event: self.console_view.on_up())
        self.console_view.super_view.root.bind("<Down>", lambda event: self.console_view.on_down())
        self.console_view.super_view.root.bind("<Left>", lambda event: self.console_view.on_left())
        self.console_view.super_view.root.bind("<Right>", lambda event: self.console_view.on_right())
        self.console_view.super_view.root.protocol("WM_DELETE_WINDOW", self.console_view.on_closing)  # 绑定关闭窗口事件
        self.console_view.super_view.root.bind("<Configure>", self.console_view.on_resize)  # 绑定窗口尺寸改变事件
        # 一、数据看板容器 ------------------------
        separator = ttk.Separator(self.parent_frame, orient="horizontal")
        separator.pack(fill="x", pady=10)
        data_frame = tk.Frame(self.parent_frame)
        data_frame.pack()
        # 第一行
        price_val_label = tk.Label(data_frame, textvariable=self.symbol_var)
        price_val_label.grid(row=0, column=0, columnspan=2)
        # 第二行
        price_label = tk.Label(data_frame, text="最新价")
        price_label.grid(row=1, column=0)
        price_val_label = tk.Label(data_frame, textvariable=self.price_var)
        price_val_label.grid(row=1, column=1)

        # 二、跳转功能面板 ------------------------
        separator = ttk.Separator(self.parent_frame, orient="horizontal")
        separator.pack(fill="x", pady=10)
        skip_frame = tk.Frame(self.parent_frame)
        skip_frame.pack()

        # 创建下一级组件
        # 第一行 -----------
        label_index = tk.Label(skip_frame, text="索引")
        label_index.grid(row=0, column=0)

        skip_frame_sub1 = tk.Frame(skip_frame)

        self.entry_index_var.set(self.console_view.console_model.index_value)
        self.entry_index = tk.Entry(skip_frame_sub1, textvariable=self.entry_index_var, width=6)
        self.entry_index.bind("<Return>", self.console_view.on_index_skip)
        self.entry_index.pack(side=tk.LEFT)

        self.label_all_index_var.set('/' + self.console_view.console_model.all_index_value)
        self.label_all_index = tk.Label(skip_frame_sub1, textvariable=self.label_all_index_var)
        self.label_all_index.pack(side=tk.LEFT)

        skip_frame_sub1.grid(row=0, column=1)

        button_index = tk.Button(skip_frame, text="跳转", command=lambda: self.console_view.on_index_skip())
        button_index.grid(row=0, column=2)
        # 第二行 -----------
        label_time = tk.Label(skip_frame, text="时间")
        label_time.grid(row=1, column=0)

        self.combobox_time_var.set('')
        self.combobox_time = ttk.Combobox(skip_frame, textvariable=self.combobox_time_var, width=18)
        self.combobox_time.bind("<Return>", self.console_view.on_time_skip)

        self.combobox_time["values"] = self.console_view.console_model.time_values

        self.combobox_time.grid(row=1, column=1)

        button_time = tk.Button(skip_frame, text="跳转", command=lambda: self.console_view.on_time_skip())
        button_time.grid(row=1, column=2)
        # 第三行 -----------
        label_signal = tk.Label(skip_frame, text="信号")
        label_signal.grid(row=2, column=0)

        skip_frame_sub1 = tk.Frame(skip_frame)

        self.entry_signal_var.set('')
        self.entry_signal = tk.Entry(skip_frame_sub1, textvariable=self.entry_signal_var, width=16)
        self.entry_signal.bind("<Return>", self.console_view.on_signal_skip)
        self.entry_signal.pack(side=tk.LEFT)

        self.label_all_signal_var.set('/' + self.console_view.console_model.all_signal_value)
        self.label_all_signal = tk.Label(skip_frame_sub1, textvariable=self.label_all_signal_var)
        self.label_all_signal.pack(side=tk.LEFT)

        skip_frame_sub1.grid(row=2, column=1)

        button_signal = tk.Button(skip_frame, text="跳转", command=lambda: self.console_view.on_signal_skip())
        button_signal.grid(row=2, column=2)

        # 三、基础操作按钮容器 ------------------------
        separator = ttk.Separator(self.parent_frame, orient="horizontal")
        separator.pack(fill="x", pady=10)
        base_frame = tk.Frame(self.parent_frame)
        base_frame.pack()

        # 第一行 -----------
        button_base_11 = tk.Button(base_frame, text="前信", width=8, command=lambda: self.console_view.on_last_signal())
        button_base_11.grid(row=0, column=0)
        button_base_12 = tk.Button(base_frame, text="放大", width=8, command=lambda: self.console_view.on_up())
        button_base_12.grid(row=0, column=1)
        button_base_13 = tk.Button(base_frame, text="后信", width=8, command=lambda: self.console_view.on_next_signal())
        button_base_13.grid(row=0, column=2)

        # 第二行 -----------
        button_base_21 = tk.Button(base_frame, text="左移", width=8, command=lambda: self.console_view.on_left())
        button_base_21.grid(row=1, column=0)
        button_base_22 = tk.Button(base_frame, text="缩小", width=8, command=lambda: self.console_view.on_down())
        button_base_22.grid(row=1, column=1)
        button_base_23 = tk.Button(base_frame, text="右移", width=8, command=lambda: self.console_view.on_right())
        button_base_23.grid(row=1, column=2)

        # 第三行 -----------
        button_base_21 = tk.Button(base_frame, text="左移5", width=8, command=lambda: self.console_view.on_left_5())
        button_base_21.grid(row=2, column=0)
        button_base_22 = tk.Button(base_frame, text=" *_* ", width=8, command=lambda: self.console_view.on_refresh())
        button_base_22.grid(row=2, column=1)
        button_base_23 = tk.Button(base_frame, text="右移5", width=8, command=lambda: self.console_view.on_right_5())
        button_base_23.grid(row=2, column=2)

        # 第四行 -----------
        button_base_31 = tk.Button(base_frame, text="刷新", width=8, command=lambda: self.console_view.on_refresh())
        button_base_31.grid(row=3, column=0)
        button_base_32 = tk.Button(base_frame, text="复位", width=16, command=lambda: self.console_view.on_reset())
        button_base_32.grid(row=3, column=1, columnspan=2)

        # 四、十字标线操作按钮容器 ------------------------
        separator = ttk.Separator(self.parent_frame, orient="horizontal")
        separator.pack(fill="x", pady=10)
        personal_frame = tk.Frame(self.parent_frame)
        personal_frame.pack()
        # 第一行 -----------
        # 显示十字线否  名称  状态  确认按钮
        label_crosshair = tk.Label(personal_frame, text="十字线")
        label_crosshair.grid(row=0, column=0)

        self.combobox_crosshair_var.set('')
        combobox_crosshair = ttk.Combobox(personal_frame, textvariable=self.combobox_crosshair_var, width=18)
        combobox_crosshair.bind("<Return>", self.console_view.on_crosshair_ok)
        combobox_crosshair["values"] = ["显示", "不显示"]
        combobox_crosshair.grid(row=0, column=1)

        button_base_11 = tk.Button(personal_frame, text="确认", width=8, command=lambda: self.console_view.on_crosshair_ok())
        button_base_11.grid(row=0, column=2)
        # 第二行 -----------
        # 左侧线以实线展示时间跨度   名称  时间值  确认按钮
        label_time_span = tk.Label(personal_frame, text="K线跨度")
        label_time_span.grid(row=1, column=0)

        self.entry_time_span_var.set('')
        entry_time_span = tk.Entry(personal_frame, textvariable=self.entry_time_span_var, width=16)
        entry_time_span.bind("<Return>", self.console_view.on_time_span_ok)
        entry_time_span.grid(row=1, column=1)

        button_base_11 = tk.Button(personal_frame, text="确认", width=8, command=lambda: self.console_view.on_time_span_ok())
        button_base_11.grid(row=1, column=2)
        # 第三行 -----------
        # 下侧线以实线展示变化幅度   名称  幅度值  确认按钮
        label_change_range = tk.Label(personal_frame, text="变化范围")
        label_change_range.grid(row=2, column=0)

        self.entry_change_range_var.set('')
        entry_change_range = tk.Entry(personal_frame, textvariable=self.entry_change_range_var, width=16)
        entry_change_range.bind("<Return>", self.console_view.on_change_range_ok)
        entry_change_range.grid(row=2, column=1)

        button_base_11 = tk.Button(personal_frame, text="确认", width=8, command=lambda: self.console_view.on_change_range_ok())
        button_base_11.grid(row=2, column=2)
        # 第四行 -----------
        # 左右移跨度   名称  幅度值  确认按钮
        label_move_unit = tk.Label(personal_frame, text="移动单位")
        label_move_unit.grid(row=3, column=0)

        self.entry_move_unit_var.set('')
        entry_move_unit = tk.Entry(personal_frame, textvariable=self.entry_move_unit_var, width=16)
        entry_move_unit.bind("<Return>", self.console_view.on_move_unit_ok)
        entry_move_unit.grid(row=3, column=1)

        button_base_11 = tk.Button(personal_frame, text="确认", width=8, command=lambda: self.console_view.on_move_unit_ok())
        button_base_11.grid(row=3, column=2)
        # 五、消息内容显示容器 ------------------------
        separator = ttk.Separator(self.parent_frame, orient="horizontal")
        separator.pack(fill="x", pady=10)
        message_frame = tk.Frame(self.parent_frame)
        message_frame.pack()
        # 第一行 消息文件
        label_message = tk.Label(message_frame, text="消息文件")
        label_message.grid(row=0, column=0)

        self.entry_message_var.set('')
        self.entry_message = tk.Entry(message_frame, textvariable=self.entry_message_var, width=16)
        self.entry_message.grid(row=0, column=1)

        button_time = tk.Button(message_frame, text="确认", command=lambda: self.console_view.on_message_file_skip())
        button_time.grid(row=0, column=2)
        # 第二行 展示消息内容
        label_message_show = tk.Label(message_frame, text="消息内容")
        label_message_show.grid(row=1, column=0)

        self.entry_message_show_var.set('')
        entry_message_show = tk.Entry(message_frame, textvariable=self.entry_message_show_var, width=30)
        entry_message_show.grid(row=1, column=1, columnspan=2)

    def fill_text(self):
        # 看板赋值
        symbol_val = self.console_view.console_model.super_model.symbol_name
        price_val = self.console_view.console_model.draw_data_k_section.iloc[-1]["close"]
        self.symbol_var.set(symbol_val)
        self.price_var.set(price_val)
        # 操作面板赋值
        all_index = "/" + self.console_view.console_model.all_index_value
        now_index = str(self.console_view.console_model.index)

        now_time_val = self.console_view.console_model.super_model.index2time(int(now_index)).strftime("%Y-%m-%d %H:%M:%S")
        time_values = self.console_view.console_model.time_values

        all_signal = "/" + self.console_view.console_model.all_signal_value
        now_signal = self.console_view.console_model.signal_value

        self.entry_index_var.set(now_index)

        self.label_all_index_var.set(all_index)

        self.combobox_time["values"] = time_values
        self.combobox_time_var.set(now_time_val)

        self.entry_signal_var.set(now_signal)

        self.label_all_signal_var.set(all_signal)


class ConsoleView:
    def __init__(self, super_view, frame, console_model):
        self.super_view = super_view
        self.frame = frame
        self.console_model = console_model
        self.console_layout = ConsoleLayout(self)

    def sec_init(self):
        pass

    def layout(self):
        self.console_layout.layout()

    def fill_text(self, event=None):
        self.console_layout.fill_text()

    def on_index_skip(self, event=None):
        self.console_model.on_index_skip()
        pass

    def on_time_skip(self, event=None):
        self.console_model.on_time_skip()
        pass

    def on_combobox_time_change(self, event=None):
        self.console_model.on_combobox_time_change()
        pass

    def on_signal_skip(self, event=None):
        self.console_model.on_signal_skip()
        pass

    def on_last_signal(self, event=None):
        self.console_model.on_last_signal()
        pass

    def on_next_signal(self, event=None):
        self.console_model.on_next_signal()
        pass

    def on_up(self, event=None):
        self.console_model.on_up()
        pass

    def on_down(self, event=None):
        self.console_model.on_down()
        pass

    def on_left(self, event=None):
        self.console_model.on_left()
        pass

    def on_right(self, event=None):
        self.console_model.on_right()
        pass

    def on_left_5(self, event=None):
        self.console_model.on_left_5()
        pass

    def on_right_5(self, event=None):
        self.console_model.on_right_5()
        pass

    def on_refresh(self, event=None):
        self.console_model.on_refresh()
        pass

    def on_reset(self, event=None):
        self.console_model.on_reset()
        pass

    def on_crosshair_ok(self, event=None):
        self.console_model.on_crosshair_ok()
        pass

    def on_time_span_ok(self, event=None):
        self.console_model.on_time_span_ok()
        pass

    def on_change_range_ok(self, event=None):
        self.console_model.on_change_range_ok()
        pass

    def on_message_file_skip(self, event=None):
        self.console_model.on_message_file_skip()
        pass

    def on_closing(self):
        self.super_view.super_model.q.put(None)
        pass

    def on_resize(self, event=None):
        self.console_model.on_resize(event)
        pass

    def on_move_unit_ok(self, event=None):
        self.console_model.on_move_unit_ok()
        pass









